package com.vertygoeclypse.multitimer;
//--------------------------------------------------------------------------------------------------
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import java.util.ArrayList;
import java.util.List;
//--------------------------------------------------------------------------------------------------
public class TimerDataBaseHandler extends SQLiteOpenHelper {
    private static final int DATABASE_VERSION = 2;
    private static final String DATABASE_NAME = "TimerManager";
    private static final String TABLE_NAME = "timers";
    private static final String KEY_ID = "_id";
    public static final String KEY_TAG = "tag";
    private static final String KEY_TIMEINSECONDS = "timeinseconds";
//--------------------------------------------------------------------------------------------------
    public TimerDataBaseHandler(Context context){
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }
//--------------------------------------------------------------------------------------------------
    @Override
    public void onCreate(SQLiteDatabase db) {
        String CREATE_TIMER_TABLE = "CREATE TABLE "+TABLE_NAME+"("+KEY_ID+" INTEGER PRIMARY KEY, "+KEY_TAG+" TEXT,"
                +KEY_TIMEINSECONDS+" TEXT"+")";
        db.execSQL(CREATE_TIMER_TABLE);
    }
//--------------------------------------------------------------------------------------------------
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
        onCreate(db);
    }
//--------------------------------------------------------------------------------------------------
    @Override
    public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
        onCreate(db);
    }
//--------------------------------------------------------------------------------------------------
    public void onDelete(SQLiteDatabase db) {
        db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
    }
//--------------------------------------------------------------------------------------------------
    public void addTimer(TimerClass timers){
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(KEY_TAG, timers.getTag());
        values.put(KEY_TIMEINSECONDS, timers.gettimeInSeconds());
        db.insert(TABLE_NAME, null, values);
        db.close();
    }
//--------------------------------------------------------------------------------------------------
    public TimerClass getTag(int id){
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.query(TABLE_NAME, new String[] {KEY_ID, KEY_TAG, KEY_TIMEINSECONDS},
                KEY_ID+"=?", new String[] {String.valueOf(id)}, null, null, null, null);
        if(cursor != null)
            cursor.moveToFirst();
        TimerClass timers = new TimerClass(Integer.parseInt(cursor.getString(0)),
                cursor.getString(1), cursor.getString(2));
        db.close();
        return timers;
        }
//--------------------------------------------------------------------------------------------------
    public List<TimerClass> getallTimers(){
        List<TimerClass> listTimers = new ArrayList<TimerClass>();
        String selectQuery = "SELECT * FROM "+TABLE_NAME;
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);
        if(cursor.moveToFirst()){
            do{
                TimerClass timers = new TimerClass();
                timers.setID(Integer.parseInt(cursor.getString(0)));
                timers.setTag(cursor.getString(1));
                timers.settimeInSeconds(cursor.getString(2));
                listTimers.add(timers);
            }     while (cursor.moveToNext());
        }
        db.close();
        return listTimers;
    }
//--------------------------------------------------------------------------------------------------
    public List<String> fetchAllTimers(){
        List<String> list = new ArrayList<String>();
        String selectQuery = "SELECT *FROM "+ TABLE_NAME;
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor =  db.rawQuery(selectQuery, null);
        if(cursor.moveToFirst()){
            do{
                list.add(cursor.getString(1));
            }while (cursor.moveToNext());
        }
        cursor.close();
        db.close();
        return list;
    }
//--------------------------------------------------------------------------------------------------
    public int getTimerCount(){
        String countQuery = "SELECT * FROM "+TABLE_NAME;
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(countQuery, null);
        cursor.close();
        db.close();
        return cursor.getCount();
    }
//--------------------------------------------------------------------------------------------------
    public void updateTimer(TimerClass timers){
        SQLiteDatabase db = this.getReadableDatabase();
        ContentValues values = new ContentValues();
        values.put(KEY_TAG, timers.getTag());
        values.put(KEY_TIMEINSECONDS, timers.gettimeInSeconds());
        db.update(TABLE_NAME, values, KEY_ID+" = ?", new String[] {String.valueOf(timers.getID())});
        db.close();
    }
//--------------------------------------------------------------------------------------------------
    public void deleteTimer(TimerClass timers){
        SQLiteDatabase db = this.getReadableDatabase();
        db.delete(TABLE_NAME, KEY_ID+" = ?", new String[]{String.valueOf(timers.getID())});
        db.close();
    }
}